package com.erbao.thread.juc;

import com.erbao.SleepUtil;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author CaoBaoYin
 * @since 2022/3/5 17:19
 */
public class CompletableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new CompletableTest().test();
    }

    public void test () throws ExecutionException, InterruptedException {
        CompletableFuture<Double> jdFuture = CompletableFuture.supplyAsync(() -> priceOfJD());
        CompletableFuture<Double> tmFuture = CompletableFuture.supplyAsync(this::priceOfTM);
        CompletableFuture<Double> tbFuture = CompletableFuture.supplyAsync(this::priceOfTB);

        // 三个线程必须都执行完
        CompletableFuture.allOf(jdFuture, tmFuture, tbFuture).join();
        // 一个线程执行完就行
        // CompletableFuture.anyOf(jdFuture, tmFuture, tbFuture).join();

        System.out.println(jdFuture.get());
        System.out.println(tmFuture.get());
        System.out.println(tbFuture.get());

        /*CompletableFuture.supplyAsync(this::priceOfTM).thenApply(String::valueOf).thenApply(str -> "price"+str).thenAccept(System.out::println);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    Random random = new Random();
    Double priceOfJD () {
        SleepUtil.sleep(1000);
        return 1.00;
    }
    Double priceOfTM () {
        SleepUtil.sleep(2000);
        return 2.00;
    }
    Double priceOfTB () {
        SleepUtil.sleep(1500);
        return 3.00;
    }

}
